/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package at.irian.demo.jsfatwork.view.page;

import at.irian.demo.jsfatwork.domain.Group;
import at.irian.demo.jsfatwork.domain.User;
import at.irian.demo.jsfatwork.service.GroupService;
import at.irian.demo.jsfatwork.view.PageBean;
import at.irian.demo.jsfatwork.view.security.UserHolder;
import org.apache.myfaces.orchestra.viewController.annotations.ViewController;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;

/**
 * @author Gerhard Petracek
 */
@PageBean
@Named("groupPage")
//add all pages which have to restart the conversation -> preparePageForRendering gets called
@ViewController(viewIds = {"/pages/groups/subscribedGroups.xhtml",
        "/pages/groups/addGroups.xhtml",
        "/pages/groups/subscribeGroups.xhtml"})
public class GroupPage extends AbstractPage
{
    @Inject
    private GroupService groupService;

    @Inject
    private UserHolder userHolder;

    private List<Group> subscribedGroups;

    private List<Group> availableGroups;

    private Group currentGroup;

    private Group selectedGroup;

    @Override
    public void preparePageForRendering()
    {
        if (this.availableGroups == null)
        {
            User currentUser = this.userHolder.loadUser();
            this.availableGroups = this.groupService.findUnSubscribedGroups(currentUser);
            this.subscribedGroups = this.groupService.findSubscribedGroups(currentUser);
            this.currentGroup = this.groupService.createNew();
        }
    }

    public void saveGroup()
    {
        this.groupService.save(this.currentGroup);
        addMessage("info_new_group_added");
        cleanup();
    }

    public void addGroup()
    {
        //don't load user via user-holder - it's a different conversation (-> different persistence context)
        this.groupService.subscribe(this.userHolder.getCurrentUserName(), this.selectedGroup);
        cleanup();
    }

    public void removeGroup()
    {
        //don't load user via user-holder - it's a different conversation (-> different persistence context)
        this.groupService.unsubscribe(this.userHolder.getCurrentUserName(), this.selectedGroup);
        cleanup();
    }

    /*
     * generated
     */
    public List<Group> getSubscribedGroups()
    {
        return subscribedGroups;
    }

    public List<Group> getAvailableGroups()
    {
        return availableGroups;
    }

    public Group getCurrentGroup()
    {
        return currentGroup;
    }

    public void setSelectedGroup(Group selectedGroup)
    {
        this.selectedGroup = selectedGroup;
    }
}
